<body></body>

<script>
	// 模拟网络
	function fetch() {
		return new Promise((resolve, reject) => {
			setTimeout(() => {
				const count = Math.random()
				const falg = count > 0.9
				console.log('重试中', count)
				falg ? resolve('网络请求成功') : reject('网络请求失败')
			}, 1000)
		})
	}
	// 一个重试机制
	function load(onError) {
		const p = fetch()
		return p.catch((err) => {
			return new Promise((resolve, reject) => {
				function retry() {
					resolve(load(onError))
				}
				function fail() {
					reject(err)
				}
				onError ? onError(retry, fail) : console.log('出现网络错误')
			})
		})
	}

	let retryCount = 10 //重试3次
	load((retry, fail) => {
		if (retryCount > 0) {
			retry()
			retryCount--
		} else {
			fail()
		}
	})
		.then((res) => {
			console.log('res', res)
		})
		.catch((err) => {
			console.log('err', err)
		})
</script>
